/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.vip.web;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.zx.user.entity.ZxUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.zx.examination.entity.Texamination;
import com.jeesite.modules.zx.examination.room.entity.TexaminationRoom;
import com.jeesite.modules.zx.examination.room.scene.entity.TexaminationRoomScene;
import com.jeesite.modules.zx.examination.room.scene.service.TexaminationRoomSceneService;
import com.jeesite.modules.zx.examination.room.service.TexaminationRoomService;
import com.jeesite.modules.zx.examination.service.TexaminationService;
import com.jeesite.modules.zx.examination.vip.entity.TexaminationVip;
import com.jeesite.modules.zx.examination.vip.service.TexaminationVipService;
import com.jeesite.modules.zx.user.vip.service.TuserVipService;

/**
 * 考试会员管理Controller
 * @author zpp
 * @version 2020-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/vip/texaminationVip")
public class TexaminationVipController extends BaseController {

	@Autowired
	private TexaminationVipService texaminationVipService;
	@Autowired
	private TexaminationService texaminationService;
	@Autowired
	private TexaminationRoomService texaminationRoomService;
	@Autowired
	private TuserVipService tuserVipService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
	private TexaminationRoomSceneService texaminationRoomSceneService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationVip get(String id, boolean isNewRecord) {
		return texaminationVipService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationVip texaminationVip, Model model) {
		model.addAttribute("texaminationVip", texaminationVip);
		return "modules/zx/examination/vip/texaminationVipList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TexaminationVip> listData(TexaminationVip texaminationVip, HttpServletRequest request, HttpServletResponse response) {
		texaminationVip.setPage(new Page<>(request, response));
		Page<TexaminationVip> page = texaminationVipService.findPage(texaminationVip);
		for(TexaminationVip vip: page.getList()){
			String examId = vip.getExamId();
			if(StringUtils.isNotBlank(examId)){
				Texamination texamination = texaminationService.get(examId);
				if(texamination!=null)
				vip.setExamName(texamination.getObjName());
			}
			String roomId=vip.getRoom();
			if(StringUtils.isNotBlank(roomId)){
				TexaminationRoom texaminationRoom = texaminationRoomService.get(roomId);
				if(texaminationRoom!=null)
				vip.setRoomName(texaminationRoom.getRoomName());
			}
			String userId=vip.getVipUser();
			if(StringUtils.isNotBlank(userId)){
				User user = UserUtils.get(userId);
				if(user!=null){
				  vip.setUserName(user.getUserName());
				}
			}
		}
		return page;
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:view")
	@RequestMapping(value = "listDataNew")
	@ResponseBody
	public Page<TexaminationVip> listDataNew(TexaminationVip texaminationVip, HttpServletRequest request, HttpServletResponse response) {
		//todo
		if (StringUtils.isNotEmpty(texaminationVip.getUserName())){
			ZxUser zxUser = new ZxUser();
			zxUser.setUserName(texaminationVip.getUserName());
			texaminationVip.setZxUser(zxUser);
		}
		if (StringUtils.isNotEmpty(texaminationVip.getExamName())){
			Texamination texamination = new Texamination();
			texamination.setObjName(texaminationVip.getExamName());
			texaminationVip.setTexamination(texamination);
		}
		texaminationVip.setPage(new Page<>(request, response));
		Page<TexaminationVip> page = texaminationVipService.findPage(texaminationVip);
		List<TexaminationVip> list = page.getList();
		if (list.size()>0){
			list.forEach(item->{
				item.setExamName(item.getTexamination()!=null?item.getTexamination().getObjName():"");
				item.setRoomName(item.getTexaminationRoom()!=null?item.getTexaminationRoom().getRoomName():"");
			    item.setUserName(item.getZxUser()!=null?item.getZxUser().getUserName():"");
			});
		}
		return page;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:view")
	@RequestMapping(value = "form")
	public String form(TexaminationVip texaminationVip, Model model) {
		model.addAttribute("texaminationVip", texaminationVip);
		Texamination examinate=new Texamination();
		LocalDateTime now=LocalDateTime.now();
		examinate.getSqlMap().getWhere().and("signup_end_time", QueryType.GTE, now).and("signup_start_time", QueryType.LTE, now);
		List<Texamination> findList = texaminationService.findList(examinate);
//		for(Texamination exam:findList){
//			String id=exam.getId();
//			TexaminationRoom roomEntiy=new TexaminationRoom();
//			roomEntiy.setExamId(id);
//			List<TexaminationRoom> findList2 = texaminationRoomService.findList(roomEntiy);
//		}
		String examId = texaminationVip.getExamId();
		List<TexaminationRoom> roomList=new ArrayList<>();
		List<TexaminationRoomScene> sceneList=new ArrayList<>();
		if(StringUtils.isNotBlank(examId)){
			TexaminationRoom roomEntiy=new TexaminationRoom();
			roomEntiy.setExamId(examId);
			roomList = texaminationRoomService.findList(roomEntiy);
			if(roomList!=null&roomList.size()>0){
				TexaminationRoomScene sceneEntity=new TexaminationRoomScene();
				sceneEntity.setExamId(examId);
				sceneEntity.setRoomId(roomList.get(0).getId());
				sceneList=texaminationRoomSceneService.findList(sceneEntity);
			}
		}
		model.addAttribute("texaminateList", findList);
		model.addAttribute("roomList", roomList);
		model.addAttribute("sceneList", sceneList);
//		TuserVip userVip=new TuserVip();
//		userVip.getSqlMap().getWhere().and("vip_endtime", QueryType.GT, now);
		

		
		//		List<TuserVip> findList2 = tuserVipService.findList(userVip);
//		List<User> userList=new ArrayList<User>();
//		for(TuserVip userV:findList2){
//			String userId = userV.getUserId();
//			User user = UserUtils.get(userId);
//			userList.add(user);
//		}
		//去除已经关联的用户
		TexaminationVip vipUser=new TexaminationVip();
		vipUser.setExamId(examId);
		List<TexaminationVip> vipUserList = texaminationVipService.findList(vipUser);
		List<String> userIdList=new ArrayList<>();
		for(TexaminationVip exaVip: vipUserList){
			String vipUser2 = exaVip.getVipUser();
			String vipUser3 = texaminationVip.getVipUser();
			if(StringUtils.isNotBlank(vipUser3)){
				if(!vipUser2.equals(vipUser3)){
					userIdList.add(vipUser2);
				}
			}else{
			    userIdList.add(vipUser2);
			}
		}
		EmpUser empUser=new EmpUser();
		empUser.getSqlMap().getWhere().and("user_code", QueryType.NOT_IN, userIdList);
		List<EmpUser> userList = empUserService.findList(empUser);
		model.addAttribute("userVipList", userList);
		return "modules/zx/examination/vip/texaminationVipForm";
	}

	/**
	 * 保存考试会员管理
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationVip texaminationVip) {
		String room = texaminationVip.getRoom();
		if(StringUtils.isNotBlank(room)){
			String examId = texaminationVip.getExamId();
			TexaminationVip vipExam=new TexaminationVip();
			vipExam.setExamId(examId);
			vipExam.setRoom(room);
			String scene=texaminationVip.getScene();
			vipExam.setScene(scene);
			Long findCount = texaminationVipService.findCount(vipExam);
			vipExam.setVipUser(texaminationVip.getVipUser());
			
			Long hasUser = texaminationVipService.findCount(vipExam);
			if(hasUser>0){
				return renderResult(Global.FALSE, text("会员已经存在"));
			}
//			判断该场次人员是否已经满员了
			if(findCount==null){
				findCount=0l;
			}
			TexaminationRoomScene texaminationRoomScene = texaminationRoomSceneService.get(scene);
			if(texaminationRoomScene!=null){
				Long personNum = texaminationRoomScene.getPersonNum();
				if(personNum==null){
					personNum=0l;
				}
				if(findCount>=personNum){
			    	return renderResult(Global.FALSE, text("该场考试人员已经到达限定人数！"));
			    }
			}
		}
		
		
		texaminationVipService.save(texaminationVip);
		return renderResult(Global.TRUE, text("保存考试会员管理成功！"));
	}
	
	/**
	 * 停用考试会员管理
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationVip texaminationVip) {
		texaminationVip.setStatus(TexaminationVip.STATUS_DISABLE);
		texaminationVipService.updateStatus(texaminationVip);
		return renderResult(Global.TRUE, text("停用考试会员管理成功"));
	}
	
	/**
	 * 启用考试会员管理
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationVip texaminationVip) {
		texaminationVip.setStatus(TexaminationVip.STATUS_NORMAL);
		texaminationVipService.updateStatus(texaminationVip);
		return renderResult(Global.TRUE, text("启用考试会员管理成功"));
	}
	
	/**
	 * 删除考试会员管理
	 */
	@RequiresPermissions("zx:examination:vip:texaminationVip:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationVip texaminationVip) {
		texaminationVipService.delete(texaminationVip);
		return renderResult(Global.TRUE, text("删除考试会员管理成功！"));
	}
	
}